from gui import gui

class application_window_fake :
    def __init__ ( self ) :
        self . _bind_event = None
        self . _bind_handler = None
        self . _bind_all_event = None
        self . _bind_all_handler = None
    def bind ( self , event , handler ) :
        self . _bind_event = event
        self . _bind_handler = handler
    def bind_all ( self , event , handler ) :
        self . _bind_all_event = event
        self . _bind_all_handler = handler

class event_handler_fake :
    _deleted_count = 0
    def __init__ ( self ) :
        self . _handled = None
    def __del__ ( self ) :
        event_handler_fake . _deleted_count += 1
    def handle ( self ) :
        self . _handled = True

class tkinter_fake :
    def __init__ ( self ) :
        self . _application_windows = [ ]
    def Tk ( self ) :
        window = application_window_fake ( )
        self . _application_windows += [ window ]
        return window

class scheduler_fake :
    def __init__ ( self ) :
        self . _run = [ ]
    def run ( self , func ) :
        self . _run += [ func ]

class mediator_fake :
    def __init__ ( self ) :
        self . _gui_any_key_event_reply = None
        self . _gui_create_application_window_reply = None
        self . _gui_focus_in_event_reply = None
        self . _gui_focus_out_event_reply = None
        self . _gui_global_mouse_button_1_event_reply = None
        self . _gui_key_backspace_event_reply = None
        self . _gui_key_ctrl_f5_event_reply = None
        self . _gui_key_down_event_reply = None
        self . _gui_key_escape_event_reply = None
        self . _gui_key_f5_event_reply = None
        self . _gui_key_return_event_reply = None
        self . _gui_key_space_event_reply = None
        self . _gui_key_up_event_reply = None
        self . _gui_mouse_button_1_event_reply = None
    def gui_any_key_event_reply ( self , handler ) :
        self . _gui_any_key_event_reply = handler
    def gui_create_application_window_reply ( self , window ) :
        self . _gui_create_application_window_reply = window
    def gui_focus_in_event_reply ( self , handler ) :
        self . _gui_focus_in_event_reply = handler
    def gui_focus_out_event_reply ( self , handler ) :
        self . _gui_focus_out_event_reply = handler
    def gui_global_mouse_button_1_event_reply ( self , handler ) :
        self . _gui_global_mouse_button_1_event_reply = handler
    def gui_key_backspace_event_reply ( self , handler ) :
        self . _gui_key_backspace_event_reply = handler
    def gui_key_ctrl_f5_event_reply ( self , handler ) :
        self . _gui_key_ctrl_f5_event_reply = handler
    def gui_key_down_event_reply ( self , handler ) :
        self . _gui_key_down_event_reply = handler 
    def gui_key_escape_event_reply ( self , handler ) :
        self . _gui_key_escape_event_reply = handler
    def gui_key_f5_event_reply ( self , handler ) :
        self . _gui_key_f5_event_reply = handler 
    def gui_key_return_event_reply ( self , handler ) :
        self . _gui_key_return_event_reply = handler
    def gui_key_space_event_reply ( self , handler ) :
        self . _gui_key_space_event_reply = handler 
    def gui_key_up_event_reply ( self , handler ) :
        self . _gui_key_up_event_reply = handler 
    def gui_mouse_button_1_event_reply ( self , handler ) :
        self . _gui_mouse_button_1_event_reply = handler

class gui_event_test_assistant :
    def __init__ ( self , test ) :
        self . _test = test
        self . _tkinter = tkinter_fake ( )
        self . _scheduler = scheduler_fake ( )
        self . _mediator = mediator_fake ( )
        self . _gui = gui ( mediator = self . _mediator , tkinter = self . _tkinter , tkfont = None , scheduler = self . _scheduler )
        event_handler_fake . _deleted_count = 0
    def check_arbitrary_event ( self , event_name , binding_type , tkinter_event_name ) :
        self . _gui . gui_create_application_window_request ( )
        window = self . _mediator . _gui_create_application_window_reply
        getattr ( self . _gui , 'gui_%s_event_request' % event_name ) ( window , None )
        self . _test . assume_equal \
            ( getattr ( self . _tkinter . _application_windows [ 0 ] , '_%s_event' % binding_type )
            , tkinter_event_name
            )
    def check_arbitrary_event_handler ( self , event_name , binding_type ) :
        handler = event_handler_fake ( )
        self . _gui . gui_create_application_window_request ( )
        window = self . _mediator . _gui_create_application_window_reply
        getattr ( self . _gui , 'gui_%s_event_request' % event_name ) ( window , handler . handle )
        getattr ( self . _tkinter . _application_windows [ 0 ] , '_%s_handler' % binding_type ) ( None )
        self . _scheduler . _run [ 0 ] ( )
        self . _test . assume_true ( handler . _handled )
    def check_arbitrary_event_handler_persistance ( self , event_name , binding_type ) :
        handler = event_handler_fake ( )
        self . _gui . gui_create_application_window_request ( )
        window = self . _mediator . _gui_create_application_window_reply
        getattr ( self . _gui , 'gui_%s_event_request' % event_name ) ( window , handler . handle )
        persisted_handler = getattr ( self . _mediator , '_gui_%s_event_reply' % event_name )
        setattr ( self . _mediator , '_gui_%s_event_reply' % event_name , None )
        delattr ( self . _tkinter . _application_windows [ 0 ] , '_%s_handler' % binding_type )
        del handler
        self . _test . assume_equal ( event_handler_fake . _deleted_count , 0 )
    def check_arbitrary_event_handler_destruction ( self , event_name , binding_type ) :
        handler = event_handler_fake ( )
        self . _gui . gui_create_application_window_request ( )
        window = self . _mediator . _gui_create_application_window_reply
        getattr ( self . _gui , 'gui_%s_event_request' % event_name ) ( window , handler . handle )
        persisted_handler = getattr ( self . _mediator , '_gui_%s_event_reply' % event_name )
        setattr ( self . _mediator , '_gui_%s_event_reply' % event_name , None )
        delattr ( self . _tkinter . _application_windows [ 0 ] , '_%s_handler' % binding_type )
        del handler
        del persisted_handler
        self . _test . assume_equal ( event_handler_fake . _deleted_count , 1 )
    def check_arbitrary_event_encapsulation ( self , event_name ) :
        handler = event_handler_fake ( )
        self . _gui . gui_create_application_window_request ( )
        window = self . _mediator . _gui_create_application_window_reply
        getattr ( self . _gui , 'gui_%s_event_request' % event_name ) ( window , handler . handle )
        persisted_handler = getattr ( self . _mediator , '_gui_%s_event_reply' % event_name )
        self . _test . assume_not_equal ( handler . handle , persisted_handler )
